package org.budo.warehouse.logic.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.budo.druid.util.DruidUtil;
import org.budo.support.dao.page.Page;
import org.budo.support.dao.page.PageModel;
import org.budo.support.javax.sql.util.JdbcUtil;
import org.budo.support.javax.sql.util.ResultSetUtil;
import org.budo.warehouse.logic.api.IDataNodeLogic;
import org.budo.warehouse.logic.util.PipelineUtil;
import org.budo.warehouse.service.api.IDataNodeService;
import org.budo.warehouse.service.entity.DataNode;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
@Component
public class DataNodeLogicImpl implements IDataNodeLogic {
    @Resource
    private IDataNodeService dataNodeService;

    @Override
    public String showGrants(DataNode dataNode) {
        try {
            String username = dataNode.getUsername();

            String password = dataNode.getPassword();
            password = null == password ? "" : password;
            password = DruidUtil.rsaDecrypt(password); // 密码解密

            Connection connection = JdbcUtil.getConnection(dataNode.getUrl(), username, password);
            PreparedStatement preparedStatement = JdbcUtil.prepareStatement(connection, "SHOW GRANTS FOR " + username);
            preparedStatement.setQueryTimeout(3);
            ResultSet resultSet = JdbcUtil.executeQuery(preparedStatement);

            List<Map<String, Object>> grantList = ResultSetUtil.toMapList(resultSet, false);

            JdbcUtil.close(resultSet);
            JdbcUtil.close(preparedStatement);
            JdbcUtil.close(connection);

            String grants = "";
            for (Map<String, Object> grant : grantList) {
                for (Entry<String, Object> entry : grant.entrySet()) {
                    String value = entry.getValue() + "";
                    int a = value.lastIndexOf(" TO ");
                    if (a > 0) {
                        value = value.substring(0, a);
                    }
                    grants += value + "<br/>";
                }
            }
            return grants + "";
        } catch (Exception e) {
            log.error("#58 showGrants error, e=" + e);
            return e + "";
        }
    }

    @Override
    public Integer dataNodeIdByCanalDestinationName(String destination) {
        PageModel<DataNode> pageModel = dataNodeService.list(Page.max());
        if (null == pageModel || null == pageModel.getList()) {
            return null;
        }

        for (DataNode dataNode : pageModel.getList()) {
            String canalDestinationName = PipelineUtil.canalDestinationName(dataNode);
            if (null != destination && null != canalDestinationName && destination.equals(canalDestinationName)) {
                return dataNode.getId();
            }
        }

        return null;
    }
}